package com.easyadmin.modules.sys.service.impl;

import com.easyadmin.common.constant.SystemConstant;
import com.easyadmin.common.context.UserContext;
import com.easyadmin.modules.sys.domain.SysRole;
import com.easyadmin.modules.sys.dto.HomeStatsDTO;
import com.easyadmin.modules.sys.dto.OrderCountDTO;
import com.easyadmin.modules.sys.mapper.BCustomerInfoMapper;
import com.easyadmin.modules.sys.mapper.BOrderDispatchMapper;
import com.easyadmin.modules.sys.service.HomeService;
import com.easyadmin.modules.sys.service.SysUserService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 〈功能概述〉<br>
 *
 * @className: HomeServiceImpl
 * @author: 程序员Mars
 * @date: 2025/2/11 22:43
 */
@Service
@AllArgsConstructor
public class HomeServiceImpl implements HomeService {

    private final BOrderDispatchMapper bOrderDispatchMapper;

    private final BCustomerInfoMapper customerInfoMapper;

    private final SysUserService sysUserService;

    public static final Integer DEFAULT_DAYS = 6;


    @Override
    public HomeStatsDTO homeStats() {
        // 获取当前用户ID
        String userId = UserContext.currentUser().getUserId();
        // 查询角色
        List<SysRole> userRoles = sysUserService.getUserRole(userId);
        boolean b = userRoles.stream().anyMatch(x -> x.getRoleCode().equals(SystemConstant.ADMIN_ROLE_CODE) && x.isChecked());
        // 非管理员设置用户ID
        if (!b) {
            if (CollectionUtils.isEmpty(userRoles)) {
                return null;
            }
            SysRole sysRole = userRoles.stream().filter(SysRole::isChecked).collect(Collectors.toList()).get(0);
            String roleCode = sysRole.getRoleCode();
            if (SystemConstant.TAKE_CODE.equals(roleCode)) {
                return bOrderDispatchMapper.selectHomeStats(userId);
            } else if (SystemConstant.SALES.equals(roleCode)) {
                return customerInfoMapper.selectHomeSalesStats(userId);
            } else if (SystemConstant.MANAGER.equals(roleCode)) {
                // 查询直播获客的统计
                return customerInfoMapper.selectHomeSalesStatsByManager(userId);
            }
        }
        return bOrderDispatchMapper.selectHomeStatsByAdmin();
    }

    @Override
    public List<OrderCountDTO> getOrderCountByDateRange(Integer days) {
        String userId = UserContext.currentUser().getUserId();
        if (days == null) {
            days = DEFAULT_DAYS;
        }
        // 查询角色
        List<SysRole> userRoles = sysUserService.getUserRole(userId);
        boolean b = userRoles.stream().anyMatch(x -> x.getRoleCode().equals(SystemConstant.ADMIN_ROLE_CODE) && x.isChecked());
        // 非管理员设置用户ID
        if (!b) {
            if (CollectionUtils.isEmpty(userRoles)) {
                return null;
            }
            SysRole sysRole = userRoles.stream().filter(SysRole::isChecked).collect(Collectors.toList()).get(0);
            String roleCode = sysRole.getRoleCode();
            if (SystemConstant.TAKE_CODE.equals(roleCode)) {
                return bOrderDispatchMapper.getOrderCountByDateRange(days, userId);
            } else if (SystemConstant.SALES.equals(roleCode)) {
                return customerInfoMapper.selectOrderCountByDateRange(days, userId);
            } else if (SystemConstant.MANAGER.equals(roleCode)) {
                return customerInfoMapper.selectOrderCountByManager(days,userId);
            }
        }
        return bOrderDispatchMapper.getOrderCountByDateRangeByAdmin(days);
    }

}